/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jeff.springWebTestNG.util;

import java.util.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 *
 * @author ajun
 *
 */
public class RedisSampleUtil {

    private static JedisPool pool;
    private static int DBIndex;
    private static String host;
    private static int port = 6379;
    private static int timeout = 60 * 1000;

    static {
        DBIndex = 0;
        host = "localhost";

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxActive(100);
        config.setMaxIdle(20);
        config.setMaxWait((long) 1000);
        config.setTestOnBorrow(false);
        pool = new JedisPool(config, host, port, timeout);		

    }

    public static void addItemToList(String key, byte[] value) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.connect();
            jedis.select(DBIndex);
            jedis.lpush(key.getBytes(), value);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                pool.returnResource(jedis);
            }
        }
    }

    @SuppressWarnings("finally")
    public static List<String> getItemFromList(String key) {
        Jedis jedis = null;
        //byte[] s=null;
        List<String> ss = null;
        try {
            jedis = pool.getResource();
            jedis.select(DBIndex);
            long len = jedis.llen(key);
            if (len == 0) {
                return null;
            }
            ss = jedis.lrange(key, 0, (int) len);
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (jedis != null) {
                pool.returnResource(jedis);
            }
            return ss;
        }

    }

    public static void addItem(String key, byte[] value) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.select(DBIndex);
            jedis.set(key.getBytes(), value);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                pool.returnResource(jedis);
            }
        }

    }

    public static byte[] getItem(String key) {
        Jedis jedis = null;
        byte[] s = null;
        try {
            jedis = pool.getResource();
            jedis.select(DBIndex);
            s = jedis.get(key.getBytes());
            return s;
        } catch (Exception e) {
            e.printStackTrace();
            return s;
        } finally {
            if (jedis != null) {
                pool.returnResource(jedis);
            }

        }


    }

    public static void delItem(String key) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.select(DBIndex);
            jedis.del(key.getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                pool.returnResource(jedis);
            }
        }

    }

    public static long getIncrement(String key) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.select(DBIndex);
            return jedis.incr(key);

        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        } finally {
            if (jedis != null) {
                pool.returnResource(jedis);
            }
        }

    }

    /**
     * 
     *
     * @param key
     * @param map
     */
    public static void setHashMap(String key, HashMap<String, String> map) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.select(DBIndex);
            if (map != null && !map.isEmpty()) {
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    jedis.hset(key, entry.getKey(), entry.getValue());
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                pool.returnResource(jedis);
            }
        }

    }

    public static Map<String, String> getHashMap(String key) {
        Map<String, String> map = new HashMap<String, String>();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.select(DBIndex);
            map = jedis.hgetAll(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                pool.returnResource(jedis);
            }
        }
        return map;

    }

    /**
     * 
     *
     * @param key
     * @param set
     */
    public static void addSet(String key, Set<String> set) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.select(DBIndex);
            if (set != null && !set.isEmpty()) {
                for (String value : set) {
                    /*
                     * for ( Iterator<String> memberItr =
                     * jedis.smembers(str).iterator();
                     * memberItr.hasNext();){ final String member =
                     * memberItr.next(); if (!jedis.sismember(str, member)){
                     * jedis.srem(str, member); }
					}
                     */
                    jedis.sadd(key, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                pool.returnResource(jedis);
            }
        }
    }

    public static Set<String> getSet(String key) {
        Set<String> sets = new HashSet<String>();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.select(DBIndex);
            sets = jedis.smembers(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                pool.returnResource(jedis);
            }
        }

        return sets;
    }
    
    public static Jedis getJedis() {
        Jedis jedis = pool.getResource();
        return jedis;
    }
    
    public static void closeJedis(Jedis jedis) {
        if (jedis != null) {
            pool.returnResource(jedis);
        }
    } 
}
